
import {reqLogin} from '../api/index'
import storageUtils from '../utils/storageUtils'
import {RECEIVE_USER,SHOW_ERROR_MSG,RESET_USER,SET_HEAD_TITLE,SET_HEAD_TITLE_ARREYS,CLOSE_TITLE} from './action_types'


/**关闭菜单标签的action */
export const closeTitle =(newtitlearreys)=>({type:CLOSE_TITLE,newtitlearreys})
/**设置头部标题的action */
export const setHeadTitle = (headTitle) => ({type: SET_HEAD_TITLE, data: headTitle})
/**设置头部数组的action */
export const setHeadTitleArreys = (headTitleArreys) => ({type: SET_HEAD_TITLE_ARREYS, data: headTitleArreys})


/*
接收用户的同步action
 */
export const receiveUser  = (user)=>({type:RECEIVE_USER,user})

/*
显示错误信息的同步action
 */
export const showErrorMsg = (errorMsg) => ({type: SHOW_ERROR_MSG, errorMsg})        

/*
登陆的异步action
 */
// export const login =  (username,password)=>{
//     return async dispatch =>{console.log('ddddaa')
//         const result = await reqLogin(username,password)
//         if(result.status === 0){
//             const user = result.data
//             storageUtils.saveUser(user)
//             dispatch(receiveUser(user))
//         }else{
//             const msg = result.msg
//             dispatch(showErrorMsg(msg))
//         }
//     }
// }
export const login = (username, password) => {
    return async dispatch => {
      const result = await reqLogin(username, password)
      if (result.status === 0) {
        const user = result.data
        storageUtils.saveUser(user)
        dispatch(receiveUser(user))
      } else {
        const msg = result.msg
        dispatch(showErrorMsg(msg))
      }
    }
  }
// 退出的action
export const logout = ()=>{
    storageUtils.removeUser()
    return {type:RESET_USER}
}